<?php

class UserModel extends DBModel
{
	protected $_table = '{{user}}';
	protected $_id = 'userid';
	
	/**
	 * 是否第一次修改
	 * @param unknown $userid
	 */
	public function firstUpdate($userid, $howmuch)
	{
		$where['select'] = 'userid';
		$where['condition'] = "userid = :userid and nickname is null and truename is null and logoid = 0 and sign is null";
		$params[':userid'] = $userid;
		if ($uid = $this->findScalar($where, $params)) {
			$data = array(
				'uid' => $uid,
				'paytype' => '赠送第一次完善资料',
				'howmuch' => $howmuch,
				'ordernum' => time(),
				'createtime' => time()
			);
			if ($this->model('Financedetail')->insert($data) !== false) {
				$this->model('UserMoney')->addMoney($uid, $howmuch);
			}
		}
	}
	
	public function getUserById($userid, $select = '*')
	{
		$where['select'] = $select;
		$where['order'] = 'orderid desc';
		$where['condition'] = 'userid = :userid';
		$params = array(':userid' => $userid);
		return $this->find($where, $params);
	}
	
	public function getUserPhoto($userid, $select = '*')
	{
		$where['from'] = '{{user}} u';
		$where['select'] = $select;
		$where['join'] = 'left join {{photo}} p on u.logoid = p.photoid';
		$where['condition'] = 'u.userid = :userid';
		$params = array(':userid' => $userid);
		return $this->find($where, $params);
	}
	
	public function getUserByUserName($username, $select = '*')
	{
		$where['select'] = $select;
		$where['condition'] = 'username = :username';
		$params = array(':username' => $username);
		return $this->find($where, $params);
	}
	
	public function getUserTrueName($userid)
	{
		$where['select'] = 'truename';
		$where['condition'] = 'userid = :userid';
		$params = array(':userid' => $userid);
		return $this->findScalar($where, $params);
	}
	
	public function getUserNickName($userid)
	{
		$where['select'] = 'nickname';
		$where['condition'] = 'userid = :userid';
		$params = array(':userid' => $userid);
		return $this->findScalar($where, $params);
	}
	
	public function checkUserName($username)
	{
		return $this->find('username = :username', array(':username' => $username));
	}
	
	public function isExistByUserName($username, $password)
	{
		$where['select'] = 'userid';
		$where['condition'] = 'username = :username AND password = :password';
		$params = array(':username' => $username, ':password' => $password);
		return $this->find($where, $params);
	}
	
	public function isExistByUserId($userid, $password)
	{
		$where['select'] = 'userid';
		$where['condition'] = 'userid = :userid AND password = :password';
		$params = array(':userid' => $userid, ':password' => $password);
		return $this->find($where, $params);
	}
	
	public function addUser($username, $password, $introrid = '', $nickname = '', $sex = 0, $hobby = '', $birthday = '', $logoid = '')
	{
		$user = array(
			'username' => $username,
			'password' => $password,
			'introrid' => $introrid,
			'nickname' => $nickname,
			'sex' => $sex,
			'hobby' => $hobby,
			'birthday' => $birthday,
			'createtime' => time(),
			'lastlogintime' => time(),
			'usertype' => USER_TYPE_MEMBER,
			'logoid' => $logoid,
			'sign' => '我注册开拍账号了噢，欢迎来订阅我！'
		);
		if (!$this->insert($user)) {
			return false;
		}
		$userid = $this->getLastInsertID();
		
		//默认关注
		$data = array(
			'buserid' => 46,
			'userid' => $userid,
			'expiretime' => strtotime('+99 month', time()),
			'createtime' => time()
		);
		$this->model('Guanzhu')->insert($data);
		
		$user_money = array(
			'userid' => $userid,
			'howmuch' => 0
		);
		$this->model('UserMoney')->insert($user_money);
		
		if (!empty($nickname) && !empty($logoid)) {
			$howmuch = 30;
			$finance = array(
				'uid' => $userid,
				'paytype' => '赠送注册时资料填写完整',
				'howmuch' => $howmuch,
				'ordernum' => time(),
				'createtime' => time()
			);
			if ($this->model('Financedetail')->insert($finance) !== false) {
				$this->model('UserMoney')->addMoney($userid, $howmuch);
			}
		}
		return true;
	}
	
	public function updateLogoByUserId($userid, $logoid)
	{
		$condition = 'userid = :userid';
		$params = array(':userid' => $userid);
		return $this->update(array('logoid' => $logoid), $condition, $params);
	}
	
	public function updatePassWord($username, $password)
	{
		$condition = 'username = :username';
		$params = array(':username' => $username);
		return $this->update(array('password' => $password), $condition, $params);
	}
	
	public function updateLoginTime($username)
	{
		$sql = 'update {{user}} set lastlogintime = '. time() .', loginnum = loginnum + 1 where username = :username';
		$params = array(':username' => $username);
		return $this->queryBySql($sql, $params);
	}
	
	public function updatePassWordByUserId($userid, $password)
	{
		$condition = 'userid = :userid';
		$params = array(':userid' => $userid);
		return $this->update(array('password' => $password), $condition, $params);
	}
	
	public function updatePassWordByUserName($username, $password)
	{
		$condition = 'username = :username';
		$params = array(':username' => $username);
		return $this->update(array('password' => $password), $condition, $params);
	}
	
	public function getGirlList($nickname = '', $order = false)
	{
		$sql = 'select u.*,um.howmuch,p.midpath from {{user}} u
				left join {{usermoney}} um on u.userid = um.userid
				left join {{photo}} p on p.photoid = u.logoid
				where u.usertype = ' . USER_TYPE_GIRL;
		if ($nickname != '') {
			$sql .= " and u.nickname like '%". $nickname ."%'";
		}
		if ($order != false) {
			$sql .= ' order by visitnum desc';
		} else {
			$sql .= ' order by u.orderid desc, u.userid desc';
		}
		return $this->queryBySql($sql, array(), 'queryAll');
	}
	
	public function isGirl($userid)
	{
		$where['select'] = 'userid';
		$where['condition'] = 'userid = :userid and usertype = 1';
		$params[':userid'] = $userid;
		return $this->findScalar($where, $params);
	}
	
	public function addVisit($userid)
	{
		$sql = 'update {{user}} set visitnum = visitnum + 1 where userid = :userid';
		return $this->queryBySql($sql, array(':userid' => $userid));
	}
	
	public function getListByIntrorid($username, $pageno, $pagesize, $select = '*')
	{
		$where['select'] = $select;
		$where['order'] = 'createtime DESC';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$where['condition'] = 'introrid = :introrid';
		$params = array();
		$params[':introrid'] = $username;
		return $this->findAll($where, $params);
	}
}